package com.menggudf.common.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    public static List<Date> getSomeDayBeforeToday(int days , Date currentDate) {
        List<Date> dates = new ArrayList<>();
        currentDate.setHours(23);
        currentDate.setMinutes(59);
        currentDate.setSeconds(59);
        dates.add(currentDate);
        for(int i = 0; i < days; i ++){
            currentDate.setHours(23);
            currentDate.setMinutes(59);
            currentDate.setSeconds(59);
            Date as = new Date(currentDate.getTime()-24*60*60*1000);
            currentDate = as ;
            dates.add(as);
        }
        return dates ;
    }
    
    public static String dateFormat(Date time){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(time);
    }
    
    public static String dateFormat1(Date time){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(time);
    }
    
    public static Long dateGap(Date d1, Date d2) {
        if (d1 == null || d2 == null) {
            throw new IllegalArgumentException("The dates must not be null");
        }
        
        d1 = clearTime(d1);
        d2 = clearTime(d2);
        return d1.getTime()/(1000*24*60*60) - d2.getTime()/(1000*24*60*60);
    }
    
    public static Date clearTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        clearTime(c);
        return c.getTime();
    }   
    
    public static Date clearTime(Calendar c) {
        if (c == null) {
            return null;
        }

        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    } 
}
